package br.com.lopes.gci.manager;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import br.com.lopes.gci.dao.IFuncionarioHibernateDAO;
import br.com.lopes.gci.exception.BusinessException;
import br.com.lopes.gci.exception.DAOException;
import br.com.lopes.gci.exception.GCIException;
import br.com.lopes.gci.exception.ManagerException;
import br.com.lopes.gci.model.Funcionario;
import br.com.lopes.gci.util.Criptografia;

@Service
public class LoginManager {
	
	@Autowired
	private IFuncionarioHibernateDAO funcionarioDAO;
	
	
	public Funcionario efetuarLogin(Funcionario funcionario) throws ManagerException, BusinessException{
		try{
			DetachedCriteria criteria = DetachedCriteria.forClass(Funcionario.class);
			criteria.add(Restrictions.eq("loginFunc", funcionario.getLoginFunc()));
			criteria.add(Restrictions.eq("senha", Criptografia.encripta(funcionario.getSenha())));
			List<Funcionario> listaFuncionarios = funcionarioDAO.findByCriteria(criteria);
			if(!listaFuncionarios.isEmpty()){
				return listaFuncionarios.get(0);
			}else{
				throw new BusinessException();
			}
		}catch(DAOException de){
			throw new ManagerException(de);
		}catch(GCIException ge){
			throw new ManagerException(ge);
		}
	}
}
